package com.mybatis.framework.factoybean;

import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;

import com.mybatis.framework.hander.MybatisInvocationHandler;

/**
 * com.mybatis.framework.factoybean
 *
 * @author humdeef
 * @since 2019-07-14
 */

public class MybatisFactoryBean implements FactoryBean {

  // 构造器需要传进来的类
  Class mapperInterface;

  public MybatisFactoryBean(Class mapperInterface) {
    this.mapperInterface = mapperInterface;
  }

  @Override
  public Object getObject() throws Exception {
    Class[] interfaces = new Class[] {mapperInterface};
    return Proxy.newProxyInstance(MybatisFactoryBean.class.getClassLoader(), interfaces,
        new MybatisInvocationHandler());
  }

  @Override
  public Class<?> getObjectType() {
    return mapperInterface;
  }

  @Override
  public boolean isSingleton() {
    return true;
  }
}
